<template>
    <div class="sqbc">
        <template v-if="msg">
            <h5 style="">{{msg}}</h5>
            <RouterLink :to="{ name: 'Index' }">重新授权</RouterLink>
        </template>
        <!-- <div v-if="hhhh.data ">userid:{{ hhhh.data.userid }}</div>
       <div v-if="hhhh.data ">data:{{ hhhh.data }}</div>  -->
       
    </div>
</template>
<script setup>
import {ref,onMounted} from "vue"
import{useRouter} from "vue-router"
import { loading,clearToast } from '@/utils/vant';
import{getAuthUserInfo,getMemberInfo} from '@/api/qy-wechat-api'
import { useUserStore } from '@/stores/modules/user'

const router = useRouter()
const userStore = useUserStore()

const msg = ref("")
//const hhhh = ref({})

onMounted(async ()=>{
    loading('正在授权中...')
    // 通过code换取网页授权access_token,获取企业微信成员id等信息
    const code= router.currentRoute.value.query.code
    const state= decodeURIComponent(router.currentRoute.value.query.state)
    const res = await getAuthUserInfo({code})
    //hhhh.value = res
    if(res.code == 200){
        // 缓存用户userid
        if(res.data.userid){
            getMemberInfo({userId:res.data.userid}) // 数据库获取成员信息
            .then(res =>{
                if(res.code == 200 && res.data){
                    userStore.setUserInfo(res.data)
                }else{
                    msg.value = '获取成员信息出错！'
                    return
                }
                router.replace(decodeURIComponent(state))
            })
        }else{
            msg.value = 'get userid error'
        }
        
        clearToast()
        
    }else{
        clearToast()
        msg.value = res.message
    } 
})
</script>

<style lang="scss" scoped>
    .sqbc{
        padding: 50px 20px;
        text-align: center;
        h5{
            text-align: center;
            font-size: 28px;
            margin: 0 0 30px 0;
        }
        a{
            display: inline-block;
            font-size: 26px;
            color: #67c23a;
        }
    }
</style>